package com.quanyue.haircut.shop.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.Activity;
import com.quanyue.haircut.shop.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "店铺活动")
@RestController
@RequestMapping("/api")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @ApiOperation(value = "查询店铺活动")
    @GetMapping(value = "/activity", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <Activity>> findCardByUser(@ApiIgnore Principal principal) {
        return ServiceResult.success(activityService.findActivity(principal.getMerchantNo()));
    }

    @ApiOperation(value = "添加店铺活动")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/activity", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Activity> createActivity(@RequestBody Activity activity, @ApiIgnore Principal principal) {
        activity.setMerchantNo(principal.getMerchantNo());
        return ServiceResult.success(activityService.createActivity(activity));
    }

    @ApiOperation(value = "更新店铺活动")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/activity", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateActivity(@RequestBody Activity activity) {
        activityService.updateActivity(activity);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除店铺活动")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/activity", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteActivity(@RequestParam Integer activityId) {
        activityService.deleteActivity(activityId);
        return ServiceResult.SUCCESS;
    }
}
